package com.cai.activiti_demo1.controller;

import com.cai.activiti_demo1.config.RedisService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
public class CountController {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;
    @GetMapping("/service/{count}")
    public String taskComplete(@PathVariable Integer count){
        ValueOperations<String, Serializable> opsForValue = redisTemplate.opsForValue();
        opsForValue.set("taskKey",count);
        String assgin= Objects.requireNonNull(opsForValue.get("taskKey")).toString();
        ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(assgin).list();
        if (taskList.isEmpty()) {
            return "当人用户没有待办任务";
        }
        for (Task task : taskList) {
            taskService.complete(task.getId());
        }
        return "待办任务执行完毕";
    }

    @GetMapping("/instance/{id}")
    private String getInstanceId(@PathVariable String id){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Map<String,Object> map=new HashMap<>();
        ProcessInstance processInstance = processEngine
                .getRuntimeService()
                .startProcessInstanceById(id);
        if (processInstance.getId().isEmpty()) {
            return "获取失败";
        }
        return "获取成功";
    }



}
